package com.wx.web.controller.course;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eachonline.core.JsonResult;
import com.wx.business.course.IxbCourseCollectService;
import com.wx.business.course.IxbCourseDetailService;
import com.wx.model.IxbCourseCollect;
import com.wx.model.IxbCourseCollectExample;
import com.wx.model.IxbCourseDetail;
import com.wx.model.IxbUser;

/**
 * 课程收藏控制器
 *
 * @author wemo
 * @Date 2018-08-08 18:27:50
 */
@Controller
@RequestMapping("/collect")
public class CourseCollectController {
	
	 @Autowired
     public  IxbCourseCollectService  ixbCourseCollectService;
	 
	 
	 @Autowired
     public  IxbCourseDetailService  ixbCourseDetailService;

    
     /**
      * 我的课程收藏
      * @param sid
      * @return
      */
    @RequestMapping(value = "/courseList")
    @ResponseBody
    public JsonResult courseList(HttpServletRequest request ) {
		IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");
		
		List<Map<String,Object>> courseList=ixbCourseCollectService.selectCourseCollectList(user.getSid());
        Map<String,Object> result=new HashMap<String,Object>();
        result.put("courseList", courseList);
		return new JsonResult(true,result);
    
    }
    
    
    /**
     * 添加课程收藏
     * @param sid
     * @return
     */
   @RequestMapping(value = "/addCourse")
   @ResponseBody
   public JsonResult addCourseCollect(HttpServletRequest request ,String courseId ) {
		IxbUser user=(IxbUser) request.getSession().getAttribute("currentAccount");
        
		IxbCourseDetail ixbCourseDetail=ixbCourseDetailService.selectByPrimaryKey(courseId);
		if(ixbCourseDetail==null)
		{
			return new JsonResult(false,"课程不存在");
		}
		IxbCourseCollectExample ext=new IxbCourseCollectExample();
		ext.createCriteria().andCourseIdEqualTo(courseId).andUserIdEqualTo(user.getSid());
		List<IxbCourseCollect> collectList=ixbCourseCollectService.selectByExample(ext);
		if(!collectList.isEmpty())
		{
			return new JsonResult(true,"添加成功");
 
		}
		ixbCourseCollectService.addCourseCollect(ixbCourseDetail, user.getSid());
		return new JsonResult(true,"添加成功");
   
   }
   
   
   /**
    * 删除课程收藏
    * @param sid
    * @return
    */
  @RequestMapping(value = "/deleteCourse")
  @ResponseBody
  public JsonResult deleteCourseCollect(HttpServletRequest request ,String collectId ) {
       
		ixbCourseCollectService.deleteByPrimaryKey(collectId);
		return new JsonResult(true,"删除成功");
  
  }
  

    
}
